package com.ss.android.video.renderview;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;

public class SSRenderTextureView extends TextureView implements TextureView.SurfaceTextureListener, IRenderView {
    private Context mContext;
    private IRenderCallback mRenderCallback;
    private WindowVisibilityChangedListener mWindowVisibilityChangedListener;

    public SSRenderTextureView(Context context) {
        this(context, null);
    }

    public SSRenderTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        this.mContext.getResources().getDisplayMetrics();
    }

    public final void resize(int width, int height) {
        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
        layoutParams.height = height;
        layoutParams.width = width;
        this.setLayoutParams(layoutParams);
    }

    public final void setRenderCallback(IRenderCallback renderCallback) {
        this.mRenderCallback = renderCallback;
        this.setSurfaceTextureListener(this);
    }

    @Override
    public SurfaceHolder getHolder() {
        return null;
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        if (this.mRenderCallback != null) {
            this.mRenderCallback.onSurfaceTextureAvailable(surfaceTexture);
        }
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return this.mRenderCallback != null && this.mRenderCallback.onSurfaceTextureDestroyed();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (this.mWindowVisibilityChangedListener != null) {
            this.mWindowVisibilityChangedListener.onWindowVisibilityChanged(visibility);
        }
    }

    @Override
    public void setWindowVisibilityChangedListener(WindowVisibilityChangedListener windowVisibilityChangedListener) {
        this.mWindowVisibilityChangedListener = windowVisibilityChangedListener;
    }
}

